package com.example.demo.controller;

import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.logging.Level;

import java.util.logging.Logger;

import static com.sun.xml.internal.ws.spi.db.BindingContextFactory.LOGGER;

public class ServerController {
    private static final int PORT = 8848;
    // 确保集合在静态代码块中初始化
    private static ObservableList<String> barcodeList = FXCollections.observableList(new ArrayList<>());
    private static ObservableList<Double> weightList = FXCollections.observableList(new ArrayList<>());
    private static ObservableList<Double> volumeList = FXCollections.observableList(new ArrayList<>());

    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(PORT);
            System.out.println("服务器已启动，等待客户端连接...");
            while (true) {
                // 等待客户端连接
                Socket clientSocket = serverSocket.accept();
                System.out.println("客户端已连接：" + clientSocket.getInetAddress());

                // 处理客户端请求
                handleClient(clientSocket);
            }
        } catch (IOException e) {
            LOGGER.log(Level.SEVERE, "服务器启动失败", e);
        }
    }

    private static void handleClient(Socket clientSocket) {
        try {
            // 用于读取客户端发送的数据
            BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            // 用于向客户端发送数据
            PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
            String inputLine;
            // 读取客户端发送的数据
            while ((inputLine = in.readLine()) != null) {
                System.out.println("接收到客户端消息：" + inputLine);
                // 解析客户端发送的数据
                String[] parts = inputLine.split(",");
                String barcode = parts[0].split("：")[1];
                double weight = Double.parseDouble(parts[1].split("：")[1]);
                double volume = Double.parseDouble(parts[2].split("：")[1]);

                // 存储数据到不同的ObservableList集合中

                    barcodeList.add(barcode);
                    System.out.println("添加单号到集合："+barcode); // 添加调试信息
                    weightList.add(weight);
                    System.out.println("添加重量到集合：" + weight); // 添加调试信息
                    volumeList.add(volume);
                    System.out.println("添加体积到集合：" + volume); // 添加调试信息




                // 向客户端发送响应消息
                out.println("数据已接收");
            }
            // 关闭输入输出流
            in.close();
            out.close();
        } catch (IOException e) {
            System.out.println("服务端启动失败："+e.getMessage());
            LOGGER.log(Level.SEVERE, "处理客户端请求时出错", e);

        } finally {
            try {
                // 关闭客户端连接
                clientSocket.close();
            } catch (IOException e) {
                LOGGER.log(Level.SEVERE, "关闭客户端连接时出错", e);

            }
        }
    }

    public static ObservableList<String> getBarcodeList() {
        return barcodeList;
    }

    public static ObservableList<Double> getWeightList() {
        return weightList;
    }

    public static ObservableList<Double> getVolumeList() {
        return volumeList;
    }
}